/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.boot.web.flux;

import com.iwindplus.boot.domain.constant.CommonConstant;
import com.iwindplus.boot.web.domain.property.CrossProperty;
import jakarta.annotation.Resource;
import java.util.Collections;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * 跨域相关配置.
 *
 * @author zengdegui
 * @since 2019/6/12
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(CrossProperty.class)
@ConditionalOnProperty(prefix = "cross", name = "enabled", havingValue = "true")
public class CrossConfiguration {
    @Resource
    private CrossProperty property;

    /**
     * 创建reactive端跨域配置.
     *
     * @return CorsWebFilter
     */
    @Bean
    public CorsWebFilter corsWebFilter() {
        final CorsConfiguration corsConfiguration = this.getCorsConfiguration();
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        urlBasedCorsConfigurationSource.registerCorsConfiguration(CommonConstant.SymbolConstant.BASE_PATH, corsConfiguration);
        CorsWebFilter corsWebFilter = new CorsWebFilter(urlBasedCorsConfigurationSource);
        log.info("CorsWebFilter={}", corsWebFilter);
        return corsWebFilter;
    }

    private CorsConfiguration getCorsConfiguration() {
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 是否发送cookie信息
        corsConfiguration.setAllowCredentials(Optional.ofNullable(this.property).map(CrossProperty::getAllowCredentials).orElse(Boolean.TRUE));
        // 有效时长
        corsConfiguration.setMaxAge(this.property.getMaxAge());
        // 允许访问的客户端域名
        corsConfiguration.setAllowedOriginPatterns(Optional.ofNullable(this.property).map(CrossProperty::getAllowedOrigins).orElse(Collections.singletonList(CommonConstant.SymbolConstant.ASTERISK)));
        // 允许服务端访问的客户端请求头
        corsConfiguration.setAllowedHeaders(Optional.ofNullable(this.property).map(CrossProperty::getAllowedHeaders).orElse(Collections.singletonList(CommonConstant.SymbolConstant.ASTERISK)));
        // 允许访问的方法名，GET POST等
        corsConfiguration.setAllowedMethods(Optional.ofNullable(this.property).map(CrossProperty::getAllowedMethods).orElse(Collections.singletonList(CommonConstant.SymbolConstant.ASTERISK)));
        return corsConfiguration;
    }
}
